package com.gxa.service.impl.cm;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.mapper.EmplMapper;
import com.gxa.mapper.cm.CustomerMapper;
import com.gxa.param.cm.QueryParam;
import com.gxa.pojo.Empl;
import com.gxa.pojo.cm.Customer;
import com.gxa.pojo.cm.vo.TableVO;
import com.gxa.service.cm.CustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author Glenn
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    CustomerMapper customerMapper;

    @Resource
    EmplMapper emplMapper;

    @Override
    public Customer getById(Integer id) {
        return customerMapper.getById(id);
    }

    @Override
    public PageInfo<Customer> listCustomer(QueryParam queryParam) {
        PageHelper.startPage(queryParam.getPage(), queryParam.getLimit());
        List<Customer> customers = customerMapper.listCustomer(queryParam);
        return new PageInfo<>(customers);
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        return customerMapper.updateStatus(id, status);
    }

    @Override
    public TableVO addCustomer(Customer customer, HttpSession session) {
        String emplName = (String) session.getAttribute("emplName");
        Empl empl = emplMapper.findByName(emplName);
        customer.setCreateEmp(empl);
        customer.setUpdateEmp(empl);
        customer.setNo(String.valueOf(System.currentTimeMillis()));
        customer.setValueLevel(1);
        customer.setCredit(3);
        customer.setAssignStatus(0);
        customer.setCreateTime(new Timestamp(System.currentTimeMillis()));
        customer.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        customerMapper.insertCustomer(customer);
        return new TableVO().setCode(200).setMsg("添加成功");
    }

    @Override
    public TableVO updateCustomer(Customer customer, HttpSession session) {
        Customer dbCustomer = customerMapper.getById(customer.getId());

        String emplName = (String) session.getAttribute("emplName");
        Empl empl = emplMapper.findByName(emplName);
        dbCustomer.setName(customer.getName());
        dbCustomer.setGender(customer.getGender());
        dbCustomer.setBirthday(customer.getBirthday());
        dbCustomer.setType(customer.getType());
        dbCustomer.setStatus(customer.getStatus());
        dbCustomer.setTel(customer.getTel());
        dbCustomer.setOfficeTel(customer.getOfficeTel());
        dbCustomer.setEmail(customer.getEmail());
        dbCustomer.setHomeTel(customer.getHomeTel());
        dbCustomer.setAddr(customer.getAddr());
        dbCustomer.setNote(customer.getNote());
        dbCustomer.setUpdateEmp(empl);
        dbCustomer.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        customerMapper.updateCustomer(dbCustomer);
        return new TableVO().setCode(200).setMsg("修改成功");
    }
}
